<?php
$id = REPLACE_Injection($_GET["id"]);
if(REPLACE_Injection($_GET["action"]) == "post-complaint"){
	$ok = NULL;
	$error = NULL;
	$image = "";
	$video = "";
	$text = utf8_encode(strip_tags(REPLACE_Injection($_POST["complaint"])));
	$partido = REPLACE_Injection($_POST["match"]);
	if(!partido){
		$error = "An error has ocurred";
	}else{
		if(strlen($text) > 0){
			$values = array(NULL, $partido, $_SESSION["user_id"], "", $text, 0, $video, 1, $id, time(), time(), 0);
			$insert = new mysqlInsert("tournament", "", "complaint_partido", $values);
			$insert->execute();
			$complaintId = $insert->getInsertedId();
			
			$img = $complaintId;
			$imgId = processImage(	IMG_MAX_ALLOWED_SIZE, 
														IMG_PATH.IMG_COMPLAINTS_PATH, 
														IMG_PATH.IMG_COMPLAINTS_PATH.IMG_THUMBNAIL_PATH, 
														IMG_THUMBNAIL_WIDTH, 
														$img, 
														"picture", 
														NULL);
			
			if($imgId != 0){
				if($imgId > 0){
					$fields = array("imagen");
					$values = array($imgId);
					$update = new mysqlUpdate("tournament", "", "complaint_partido", $fields, $values, "`id`='".$complaintId."'");
					$update->execute();					
				}else{
					$delete = new mysqlDelete("tournament", "", "complaint_partido", "`id`='".$complaintId."'");
					$delete->execute();
					
					if($imgId == -1){
						$error = "The uploaded picture has a not valid format. Use only JPG or PNG files";
					}else if($imgId == -2){
						$error = "An error has ocurred, please try again later or post a bug in the correct forum section";
					}
				}				
			}
			
			if(empty($error)){
				$ok = "Your complaint has been posted";
			}
		}else{
			$error = "You have to fill the text field";
		}
	}
	
}
?>
<div id="all-tournament">
<?php mostrarComplaints($id, $ok, $error);?>
</div>